package com.xbongbong.pro.statistics.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.helper.ExecutorProcessHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.statistic.result.pojo.dto.ProjectDTO;
import com.xbongbong.pro.statistic.result.pojo.dto.ProjectDataSetDTO;
import com.xbongbong.pro.statistic.result.pojo.dto.StartDolphinProcessDTO;
import com.xbongbong.pro.statistic.service.DolphinConnService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 描述信息
 *
 * @ClassName DolphinConnController
 * @Author 梁鲁江
 * 创建时间 2022/6/16 16:25
 * 修改时间  2022/6/16 16:25
 **/
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/chart/dolphin")
public class DolphinConnController {
    @Resource
    private DolphinConnService dolphinConnService;
    @Resource
    private ExecutorProcessHelper executorProcessHelper;
    @RequestMapping(value = "/createProcess", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String createProcess(@RequestBody @Valid ProjectDataSetDTO projectDataSetDTO, BindingResult br) throws XbbException {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            BaseVO baseVO = new BaseVO();
            //创建项目
            dolphinConnService.createProcess(projectDataSetDTO);
            response = new XbbResponse<>(baseVO);
           /* try {
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }*/
        }
        return JSON.toJSONString(response);
    }

    

    @RequestMapping(value = "/startProcess", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String startProcess(@RequestBody @Valid StartDolphinProcessDTO startProcessDTO, BindingResult br) throws XbbException {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            BaseVO baseVO = new BaseVO();
            executorProcessHelper.startProcessInstance(startProcessDTO.getProjectCode(),startProcessDTO.getProcessDefinitionCode());
            response = new XbbResponse<>(baseVO);
 
        }
        return JSON.toJSONString(response);
    }
}
